<?php
    header('Content-type: text/html; charset=utf-8');
    error_reporting(E_ALL); 
    date_default_timezone_set("Asia/Saigon");
    require_once("config.php");     
    require_once("functions.php");     

    $day= date('d',time());
    $month= intval(date('m',time()));
    $year= date('Y',time()); 
    
    $day= intval($day);
   /*echo $day;die;
    $day= 10;
    $month= 1;
    $year= 2014;*/
    
    $rateS=getRateSms($month,$year); 
    $rateC=getRateCard($month,$year); 
    $index_rate = $month. "_" .$year;
    $arrUser = getUserIdPaymentInDay($day,$month,$year);
    foreach($arrUser as $row){
        
        /*--Ghep mang type payment--*/
        $arrTypeSms = getTypePaymentInDayByUserId($day,$month,$year,$row);
        $arrTypeCard = getTypePaymentCardInDayByUserId($day,$month,$year,$row);
        $arr =array();
        $arr= array_merge($arrTypeSms,$arrTypeCard);
        $arrType = array();
        foreach($arr as $type ){
          $arrType[$type['type']]=$type['type'];  
        }
        /*--End--*/
        
        foreach($arrType as $value){
            
           // var_dump("Type -->".$value."<br>");
            $dataSms= getSMSLogInDayByUserIdAndType($day,$month,$year,$row,$value)  ; 
            $dataCard= getCardLogInDayByUserIdAndType($day,$month,$year,$row,$value)  ; 
            
            $revenue_member=0;
            $revenue_member_sms=0;
            $revenue_member_card=0;
            
            $revenue_partner=0;
            $revenue_partner_sms=0;
            $revenue_partner_card=0;
            
            $revenue_system=0;
            
            $revenue_sds=0;
            $revenue_sds_sms=0;
            $revenue_sds_card=0;
            
            $total_sms_in_type =0;
            $total_card_in_type =0;  
            foreach($dataSms as $key) {
                
               // var_dump("key -->".var_dump($key)."<br>");
                $sc = preg_replace("/(\d+)(\d+)(\d+)(\d+)/si", "X$2XX", $key['serviceNumber']);
                $rateSms=$rateS[$index_rate][$sc]/100;
                
              //  echo("rateSms -->".($rateSms)."<br>");
                if($key['type'] !=1 && $key['type'] !=2){
                    $money_member_sms=  $key['count']* $rateSms* $key['price']*0.85*$key['percent_wapmaster'] /100 ;
                } else{
                    $money_member_sms=  $key['count']* $rateSms* $key['price']*0.85*0.9*$key['percent_wapmaster'] /100 ;
                }
              //  echo("money_member_sms -->".($money_member_sms)."<br>");
                
                $money_partner_sms= $key['count']* $rateSms* $key['price']*$key['percent_partner'] /100 ;
                $money_sds_sms =  $key['count']* $rateSms* $key['price']*0.85;

                $revenue_member_sms=$revenue_member_sms+ $money_member_sms;
                $revenue_partner_sms=$revenue_partner_sms+ $money_partner_sms;
                $revenue_sds_sms=$revenue_sds_sms+ $money_sds_sms;
                $total_sms_in_type = $total_sms_in_type+ $key['count'] ;
            }
            foreach($dataCard as $keyCard) {
                $rateCard=$rateC[$index_rate]/100;
                $money_member_card=  $keyCard['count']* $rateCard* $keyCard['price']*$keyCard['percent_wapmaster'] /100 ;
                $money_partner_card= $keyCard['count']* $rateCard* $keyCard['price']*$keyCard['percent_partner'] /100 ;
                $money_sds_card =  $keyCard['count']* $rateCard* $keyCard['price'];

                $revenue_member_card=$revenue_member_card+ $money_member_card;
                $revenue_partner_card=$revenue_partner_card+ $money_partner_card;
                $revenue_sds_card=$revenue_sds_card+ $money_sds_card;
                $total_card_in_type = $total_card_in_type+ $keyCard['count'] ;
            } 
            
            $revenue_member =  $revenue_member_sms+    $revenue_member_card;
            $revenue_partner =   $revenue_partner_sms+   $revenue_partner_card;
            $revenue_sds =    $revenue_sds_sms+  $revenue_sds_card;
            $revenue_system =      $revenue_sds-      $revenue_member -   $revenue_partner;
            /*Insert, update thong ke*/
            $check = getRevenueByUserIdAndType($row,$value,$day,$month,$year);
            if(empty($check)) {
                $arrInsert=array();
                $arrInsert['userId']= $row;
                $arrInsert['type']= $value;
                $arrInsert['sms']=$total_sms_in_type;
                $arrInsert['card']=$total_card_in_type;
                $arrInsert['revenue_member']=ceil($revenue_member);
                $arrInsert['revenue_system']=ceil($revenue_system);
                $arrInsert['revenue_partner']=ceil($revenue_partner);
                $result = insertRevenueByUserIdAndTypeAndDate($arrInsert,$day,$month,$year);
                if($result>0){
                    echo "Cront Job insert success at ".date("Y/m/d H:i:s",time())."</br>"; 
                }
            } else{
                $arrUpdate=array();
                $arrUpdate['userId']= $row;
                $arrUpdate['type']= $value;
                $arrUpdate['sms']=$total_sms_in_type;
                $arrUpdate['card']=$total_card_in_type;
                $arrUpdate['revenue_member']=ceil($revenue_member);
                $arrUpdate['revenue_system']=ceil($revenue_system);
                $arrUpdate['revenue_partner']=ceil($revenue_partner);
                $result = updateRevenueByUserIdAndTypeAndDate($arrUpdate,$day,$month,$year);
                if($result>0){
                    echo "Cront Job update success at ".date("Y/m/d H:i:s",time())."</br>";
                }  
            }
            /*var_dump($revenue_telco);
            var_dump($revenue_partner);
            var_dump($revenue_member);
            var_dump($revenue_system); die;  */
        }
    }
?>
